package com.relytosoft.mizar.media.camera.services.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.mizar.media.camera.api.entity.CameraBase;
import com.relytosoft.mizar.media.camera.api.entity.CameraType;
import com.relytosoft.mizar.media.camera.api.pojo.dto.CameraTypeDto;
import com.relytosoft.mizar.media.camera.api.pojo.vo.CameraTypeVo;
import com.relytosoft.mizar.media.camera.services.mapper.CameraTypeMapper;
import com.relytosoft.mizar.media.camera.services.service.CameraBaseService;
import com.relytosoft.mizar.media.camera.services.service.CameraTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author lj
 */
@Service
public class CameraTypeServiceImpl extends ServiceImpl<CameraTypeMapper, CameraType> implements CameraTypeService {

    @Resource
    private CameraBaseService cameraBaseService;

    @Resource
    private CameraTypeMapper cameraTypeMapper;

    @Override
    public boolean add(CameraTypeVo cameraTypeVo) {
        CameraType cameraType = new CameraType();
        BeanUtil.copyProperties(cameraTypeVo, cameraType);
        CameraType type = findByName(cameraType.getName());
        if (type == null) {
            return this.save(cameraType);
        }
        return false;
    }

    @Override
    public IPage<CameraType> selectTypePageList(CameraTypeDto cameraTypeDto, PageQuery pageQuery) {
        CameraType cameraType = BeanCopyUtils.copy(cameraTypeDto, CameraType.class);
        LambdaQueryWrapper<CameraType> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(!StrUtil.isEmptyIfStr(cameraType.getName()), CameraType::getName, cameraType.getName());
        Page<CameraType> page = this.page(pageQuery.build(), wrapper);
        List<CameraType> cameraTypeList = page.getRecords();
        Set<Integer> set = cameraTypeList.stream().map(CameraType::getIcon).collect(Collectors.toSet());
        List<CameraType> cameraUrls = cameraTypeMapper.selectIconUrl(set);
        for (CameraType type : cameraTypeList) {
            for (CameraType url : cameraUrls) {
                if (type.getIcon() != null && type.getIcon().equals(url.getId())) {
                    type.setUrl(url.getUrl());
                }
            }
        }
        return page;
    }

    @Override
    public void deleteCameraTypeByIds(Integer[] typeIds) {
        //获取有绑定设备得类型
        LambdaQueryWrapper<CameraBase> wrapper = Wrappers.lambdaQuery();
        wrapper.in(CameraBase::getCameraTypeId, Arrays.asList(typeIds));
        List<CameraBase> cameraBaseList = cameraBaseService.list(wrapper);
        Set<Integer> bindCamera = cameraBaseList.stream().map(CameraBase::getCameraTypeId).collect(Collectors.toSet());

        //去重
        Set<Integer> typeSets = Arrays.stream(typeIds).collect(Collectors.toSet());
        typeSets.removeAll(bindCamera);

        //删除
        this.removeBatchByIds(typeSets);

    }

    @Override
    public CameraType findByName(String name) {
        LambdaQueryWrapper<CameraType> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(!StrUtil.isEmptyIfStr(name), CameraType::getName, name);
        return this.getOne(wrapper);
    }

    @Override
    public boolean updateCameraType(CameraType cameraType) {
        return this.updateById(cameraType);
    }

    @Override
    public CameraType byId(Integer id) {
        return this.getById(id);
    }
}
